var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    categoryList: [],
    currentCategory: {},
    currentSubCategoryList: {},
    goodsList:[],
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0
  },
  onLoad: function(options) {
    this.getCatalog();
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getCatalog();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  getCatalog: function() {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.CatalogList).then(function(res) {
      console.log(res)
      that.setData({
        categoryList: res.data.categoryList,
        currentCategory: res.data.currentCategory,
        currentSubCategoryList: res.data.currentSubCategory
      });
      
      that.getCurrentGoodsList(res.data.categoryList[0].id);
      wx.hideLoading();
    });
    util.request(api.GoodsCount).then(function(res) {
      that.setData({
        goodsCount: res.data
      });
    });

  },
  getCurrentCategory: function(id) {
    let that = this;
    util.request(api.CatalogCurrent, {
        id: id
      })
      .then(function(res) {
        console.log(res)
        that.setData({
          currentCategory: res.data.currentCategory,
          currentSubCategoryList: res.data.currentSubCategory
        });
      });
  },
  getCurrentGoodsList: function(id) {
    let that = this;
    util.request(api.GoodsList, {
      categoryId: id,
      getProduct: 1
      })
      .then(function(res) {
        console.log(res)
        that.setData({
          goodsList: res.data.list
        });
      });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  switchCate: function(event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }

    this.getCurrentCategory(event.currentTarget.dataset.id);
    this.getCurrentGoodsList(event.currentTarget.dataset.id);
  },
  add_cart:function(event){
    var that = this;
    var currentTarget = event.currentTarget.dataset;
    console.log(currentTarget)
    var id = currentTarget.id;
    var productid = currentTarget.productid;
    var number = currentTarget.num;
    var brandid = currentTarget.brandid;
    wx.showToast({
      title: '加入购物车...',
      icon: 'loading'
    });
    util.request(api.CartAdd, {
      goodsId: id,
      productId: productid,
      number:number,
      brandId:brandid
      }, "POST")
      .then(function(res) {
        console.log(res)
        if(res.errno == 0){
          wx.showToast({
            title: '加入成功'
          });
        }else{
          wx.showToast({
            title: res.errmsg,
            icon: 'loading',
            duration: 1500
          });
        }
      });
  }
})